using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

using SBPweb.Themes.Windows.Skins;
using SBPweb.Themes.Windows;

namespace SBPweb.Controls.Windows.Form
{
	/// <summary>
	/// Class of SmallFormFrameBottom
	/// </summary>
	[ToolboxItem(false)]
	public partial class SmallFormFrameBottom : UserControl, ISkinnable
	{
		#region Private members

		private bool mUseDefaultSkin;
		private Skin mSkin;
		private Margin mSizingMargin;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Constructor of SmallFormFrameBottom
		/// </summary>
		public SmallFormFrameBottom()
		{
			InitializeComponent();
			InitializeMargins();
			mSizingMargin = new Margin();
			SetStyle();
			RegisterEventHandlers();
		}

		/// <summary>
		/// Constructor of SmallFormFrameBottom
		/// </summary>
		/// <param name="skin">skin</param>
		public SmallFormFrameBottom(Skin skin)
		{
			mSkin = skin;
			InitializeMargins();
			SetStyle();
			RegisterEventHandlers();
		}

		/// <summary>
		/// Constructor of SmallFormFrameBottom
		/// </summary>
		/// <param name="container">parent container</param>
		public SmallFormFrameBottom(IContainer container)
		{
			container.Add(this);
			InitializeComponent();
			InitializeMargins();
			mSizingMargin = new Margin();
			SetStyle();
			RegisterEventHandlers();
		}

		private void SetStyle()
		{
			base.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true);
		}

		private void RegisterEventHandlers()
		{
			DefaultLookAndFeel.OnSkinChanged += new EventHandler(DefaultLookAndFeel_OnSkinChanged);
		}

		private void InitializeMargins()
		{
			if (Skin == null)
				mSizingMargin = new Margin();
			else
				mSizingMargin = new Margin(Skin.SelectSingleNode("Elements/SmallFormFrameBottom/Image/SizingMargins"));
		}

		#endregion

		#region Custom draw

		/// <summary>
		/// On paint
		/// </summary>
		/// <param name="e">event arg</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			if (Skin == null)
			{
				base.OnPaint(e);

				return;
			}

			base.OnPaintBackground(e);

			int imgCurrent = 0;

			#region Window parts
			Rectangle tLT = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top);
			Rectangle tT = new Rectangle(this.ClientRectangle.Left + Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left, this.ClientRectangle.Top, this.ClientRectangle.Width - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Horizontal, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top);
			Rectangle tRT = new Rectangle(this.ClientRectangle.Right - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right, this.ClientRectangle.Top, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top);
			Rectangle tLM = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top + Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left, this.ClientRectangle.Height - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Vertical);
			Rectangle tM = new Rectangle(this.ClientRectangle.Left + Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left, this.ClientRectangle.Top + Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top, this.ClientRectangle.Width - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Horizontal, this.ClientRectangle.Height - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Vertical);
			Rectangle tRM = new Rectangle(this.ClientRectangle.Right - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right, this.ClientRectangle.Top + Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right, this.ClientRectangle.Height - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Vertical);
			Rectangle tLB = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Bottom - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom);
			Rectangle tB = new Rectangle(this.ClientRectangle.Left + Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left, this.ClientRectangle.Bottom - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom, this.ClientRectangle.Width - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Horizontal, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom);
			Rectangle tRB = new Rectangle(this.ClientRectangle.Right - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right, this.ClientRectangle.Bottom - Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right, Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom);
			#endregion

			#region Drawing

			Image iLT = Skin.CachedProperties.SmallFormFrameBottomImageLT[imgCurrent];
			Image iT = Skin.CachedProperties.SmallFormFrameBottomImageT[imgCurrent];
			Image iRT = Skin.CachedProperties.SmallFormFrameBottomImageRT[imgCurrent];
			Image iLM = Skin.CachedProperties.SmallFormFrameBottomImageLM[imgCurrent];
			Image iM = Skin.CachedProperties.SmallFormFrameBottomImageM[imgCurrent];
			Image iRM = Skin.CachedProperties.SmallFormFrameBottomImageRM[imgCurrent];
			Image iLB = Skin.CachedProperties.SmallFormFrameBottomImageLB[imgCurrent];
			Image iB = Skin.CachedProperties.SmallFormFrameBottomImageB[imgCurrent];
			Image iRB = Skin.CachedProperties.SmallFormFrameBottomImageRB[imgCurrent];

			e.Graphics.PixelOffsetMode = PixelOffsetMode.None;

			if (Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left != 0 && Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top != 0)
				e.Graphics.DrawImage(iLT, tLT, new Rectangle(1, 1, iLT.Width - 2, iLT.Height - 2), GraphicsUnit.Pixel);
			if (Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top != 0)
				e.Graphics.DrawImage(iT, tT, new Rectangle(1, 1, iT.Width - 2, iT.Height - 2), GraphicsUnit.Pixel);
			if (Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right != 0 && Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Top != 0)
				e.Graphics.DrawImage(iRT, tRT, new Rectangle(1, 1, iRT.Width - 2, iRT.Height - 2), GraphicsUnit.Pixel);
			if (Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left != 0)
				e.Graphics.DrawImage(iLM, tLM, new Rectangle(1, 1, iLM.Width - 2, iLM.Height - 2), GraphicsUnit.Pixel);
			e.Graphics.DrawImage(iM, tM, new Rectangle(1, 1, iM.Width - 2, iM.Height - 2), GraphicsUnit.Pixel);
			if (Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right != 0)
				e.Graphics.DrawImage(iRM, tRM, new Rectangle(1, 1, iRM.Width - 2, iRM.Height - 2), GraphicsUnit.Pixel);
			if (Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Left != 0 && Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom != 0)
				e.Graphics.DrawImage(iLB, tLB, new Rectangle(1, 1, iLB.Width - 2, iLB.Height - 2), GraphicsUnit.Pixel);
			if (Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom != 0)
				e.Graphics.DrawImage(iB, tB, new Rectangle(1, 1, iB.Width - 2, iB.Height - 2), GraphicsUnit.Pixel);
			if (Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Right != 0 && Skin.CachedProperties.SmallFormFrameBottomSizingMargin.Bottom != 0)
				e.Graphics.DrawImage(iRB, tRB, new Rectangle(1, 1, iRB.Width - 2, iRB.Height - 2), GraphicsUnit.Pixel);

			#endregion
		}

		#endregion

		#region Events

		private void DefaultLookAndFeel_OnSkinChanged(object sender, EventArgs e)
		{
			InitializeMargins();
			SetStyle();
			Invalidate(true);
		}

		#endregion

		#region ISkinnable Members

		/// <summary>
		/// Use default skin?
		/// </summary>
		[Category("Look && feel")]
		public bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				mUseDefaultSkin = value;
				DefaultLookAndFeel_OnSkinChanged(this, null);
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		public Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				mSkin = value;
				Invalidate();
			}
		}

		#endregion
	}
}


